<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="维护日期">
        <el-date-picker
          v-model="daterangeMaintenanceDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维护类型" prop="maintenanceType">
        <el-select v-model="queryParams.maintenanceType" placeholder="请选择维护类型">
          <el-option
            v-for="item in maintenanceType"
            :key="item.id"
            :label="item.dictLabel"
            :value="item.dictLabel"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维护人" prop="maintenanceMan">
        <el-input
          v-model="queryParams.maintenanceMan"
          placeholder="请输入维护人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类别" prop="deviceCategory">
        <el-select @change="selectChanged" v-model="queryParams.deviceCategory" placeholder="请选择设备种类" clearable size="small">
          <el-option
            v-for="dict in deviceCategoryOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="维护结果" prop="result">
        <el-select v-model="queryParams.result" placeholder="请选择维护结果" clearable size="small">
          <el-option
            v-for="dict in resultOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['security:afMaintenance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['security:afMaintenance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['security:afMaintenance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['security:afMaintenance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afMaintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="维护日期" align="center" prop="maintenanceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="维护类型" align="center" prop="maintenanceType"/>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="维护人" align="center" prop="maintenanceMan" />
      <el-table-column label="设备地址" align="center" prop="deviceAddr" />
      <el-table-column label="设备种类" align="center" prop="deviceCategory" :formatter="deviceCategoryFormat" />
      <el-table-column label="维护结果" align="center" prop="result" :formatter="resultFormat" />
      <el-table-column label="服务单位" align="center" prop="serviceUnit" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['security:afMaintenance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['security:afMaintenance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运行维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="维护日期" prop="maintenanceDate">
          <el-date-picker clearable size="small"
            v-model="form.maintenanceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择维护日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维护类型" prop="maintenanceType">
          <el-select v-model="form.maintenanceType" placeholder="请选择维护类型">
            <el-option
              v-for="item in maintenanceType"
              :key="item.id"
              :label="item.dictLabel"
              :value="item.dictLabel"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input disabled v-model="form.deviceCode" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备类别" prop="deviceCategory">
          <el-select @change="selectChanged" v-model="form.deviceCategory" placeholder="请选择设备种类">
            <el-option
              v-for="dict in deviceCategoryOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维护设备" prop="deviceName">

          <el-select filterable v-model="form.deviceName" placeholder="请选择设备种类">
            <el-option

              @click.native="selectdevice(dict)"
              v-for="dict in deviceList"
              :key="dict.deviceCode"
              :label="dict.showValue"
              :value="dict.deviceName"
            ></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="维护人" prop="maintenanceMan">
          <el-input v-model="form.maintenanceMan" placeholder="请输入维护人" />
        </el-form-item>
        <el-form-item label="维护结果" prop="result">
          <el-select v-model="form.result" placeholder="请选择维护结果">
            <el-option
              v-for="dict in resultOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务单位" prop="serviceUnit">
          <el-input v-model="form.serviceUnit" placeholder="请输入三方服务单位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAfMaintenance, getAfMaintenance, delAfMaintenance, addAfMaintenance, updateAfMaintenance, getAllDeviceByType } from "@/api/smartParks/security/securityMaintain";
import Editor from '@/components/Editor';
import { listData } from '@/api/system/dict/data'

export default {
  name: "AfMaintenance",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运行维护表格数据
      afMaintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 维护日期时间范围
      daterangeMaintenanceDate: [],
      // 设备种类字典
      deviceCategoryOptions: [],
      // 维护结果字典
      resultOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maintenanceDate: null,
        deviceCode: null,
        maintenanceType: null,
        deviceName: null,
        maintenanceMan: null,
        deviceAddr: null,
        content: null,
        deviceCategory: null,
        result: null,
        serviceUnit: null,
      },
      maintenanceType:[],
      deviceList:[],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceCode: [
          { required: true, message: "请选择设备", trigger: "change" }
        ],
        maintenanceDate:[
          {required: true, message: "请选择维保日期", trigger: "change"}
        ],
        result:[
          {required: true, message: "请选择维保结果", trigger: "change"}
        ],
        maintenanceType:[
          {required: true, message: "请选择维保类型", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("device_category").then(response => {
      this.deviceCategoryOptions = response.data;
    });
    this.getDicts("maintenance_status").then(response => {
      this.resultOptions = response.data;
    });
    listData({ dictType: "maintenance_type" }).then((response) => {
      this.maintenanceType = response.rows;
    });
  },
  methods: {


    selectdevice(data){
      this.form.deviceCode = data.deviceCode;
      this.form.deviceId = data.deviceId;
      this.form.deviceName = data.deviceName;
      this.form.deviceAddr = data.deviceAddr;
    },

    firstLoadChange(data){
      getAllDeviceByType(data).then(response =>{
        this.deviceList = response.data
      })
    },

    //类型变化
    selectChanged(data){
      getAllDeviceByType(data).then(response =>{
        this.deviceList = response.data
        this.form.deviceCode = "";
        this.form.deviceId = "";
        this.form.deviceName = "";
        this.form.deviceAddr = "";
      })
    },


    /** 查询运行维护列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeMaintenanceDate && '' != this.daterangeMaintenanceDate) {
        this.queryParams.params["beginMaintenanceDate"] = this.daterangeMaintenanceDate[0];
        this.queryParams.params["endMaintenanceDate"] = this.daterangeMaintenanceDate[1];
      }
      listAfMaintenance(this.queryParams).then(response => {
        this.afMaintenanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 设备种类字典翻译
    deviceCategoryFormat(row, column) {
      return this.selectDictLabel(this.deviceCategoryOptions, row.deviceCategory);
    },
    // 维护结果字典翻译
    resultFormat(row, column) {
      return this.selectDictLabel(this.resultOptions, row.result);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        maintenanceDate: null,
        deviceCode: null,
        maintenanceType: null,
        deviceName: null,
        deviceId: null,
        maintenanceMan: null,
        deviceAddr: null,
        content: null,
        deviceCategory: null,
        result: null,
        serviceUnit: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeMaintenanceDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运行维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAfMaintenance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运行维护";

        this.firstLoadChange(this.form.deviceCategory)

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAfMaintenance(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAfMaintenance(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除运行维护编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAfMaintenance(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('security/afMaintenance/export', {
        ...this.queryParams
      }, `security_afMaintenance.xlsx`)
    }
  }
};
</script>
